<?php

class Guru extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('model_user');
        if($this->model_user->auth() == 0 || $this->model_user->auth() !== 1){
            redirect('login/index');
        }else{
            if($this->session->userdata('type') == 'guru'){
                redirect('guru');
            }else if($this->session->userdata('type') == 'siswa'){
                redirect('siswa');
            }
        }
        $this->load->model('admin/Model_guru');
    }

    function index() {
        $data['main_content'] = 'admin/view_guru/inputan';
        $data['query'] = $this->Model_guru->getAll();
        $this->load->view('includes/template', $data);
    }
    
    function show(){
        $data['query'] = $this->Model_guru->getAll();
        $this->load->view('admin/view_guru/show', $data);
    }

    function submit() {
        if ($this->input->post('submit')) {

            if ($this->input->post('id')) {
                $this->form_validation->set_rules('nip', 'Nip', 'required|min_length[5]|max_length[12]');
                $this->form_validation->set_rules('password', 'Password', 'matches[passconf]');
                $this->form_validation->set_rules('nama', 'Nama', 'required|min_length[5]|max_length[25]');

                if ($this->form_validation->run() == FALSE) {
                    $data['id'] = $this->input->post('id');
                    $data['nip'] = set_value('nip');
                    $data['nama'] = set_value('nama');
                    $this->load->view('admin/view_guru/edit', $data);
                } else {
                    $this->Model_guru->update();
                    echo "<script type='text/javascript'>window.top.window.alertz('alert-success','Sukses !', 'Data berhasil diupdate');</script>";
                    //redirect('guru/index');
                }
            } else {
                $this->form_validation->set_rules('nip', 'Nip', 'required|min_length[5]|max_length[12]');
                $this->form_validation->set_rules('password', 'Password', 'matches[passconf]');
                $this->form_validation->set_rules('nama', 'Nama', 'required|min_length[5]|max_length[25]');
                $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');

                if ($this->form_validation->run() == FALSE) {
                    $data['query'] = $this->Model_guru->getAll();
                    $this->load->view('admin/view_guru/input', $data);
                } else {
                    if($this->Model_guru->save()){
                        echo "<script type='text/javascript'>window.top.window.alertz('alert-success','Sukses !', 'Data berhasil dimasukkan');</script>";
                    }else{
                        echo "<script type='text/javascript'>window.top.window.alertz('alert-error','Gagal !', 'Data gagal masuk');</script>";
                    }
                }
            }
        }
    }

    function input() {
        $this->load->view('admin/view_guru/input');
    }

    function edit() {
        $id = $this->uri->segment(4);
        $data['result'] = $this->Model_guru->getGuru($id);

        if (empty($id) or count($data['result']) == 0) {
            redirect('admin/guru/index');
        } else {
            $result = $this->Model_guru->getGuru($id);
            $data['id'] = $result['id'];
            $data['nip'] = $result['nip'];
            $data['nama'] = $result['nama'];
            $this->load->view('admin/view_guru/edit', $data);
        }
    }
    
    function delconf(){
        $id = $this->uri->segment(4);
        $data['result'] = $this->Model_guru->getGuru($id);

        if (empty($id) or count($data['result']) == 0) {
            redirect('admin/guru/index');
        } else {
            $result = $this->Model_guru->getGuru($id);
            $data['id'] = $result['id'];
            $data['nip'] = $result['nip'];
            $data['nama'] = $result['nama'];
            $this->load->view('admin/view_guru/del', $data);
        }
    }

    function delete($id) {
        $this->db->delete('guru', array('id' => $id));
        echo "<script type='text/javascript'>window.top.window.alertz('alert-success','Sukses !', 'Data berhasil dihapus');</script>";
        //$this->load->view('admin/view_guru/show', $data);
    }

}

?>